<!DOCTYPE html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>Document</title>
</head>
<style>
  .box {
    background-color: green;
    /* padding: 25% 0; */
    /* 前提宽度是 viewport */
    /* height: 50vw; */
  }
</style>
<body>
  <div class="box"></div>
  <script>
    const oBox = document.querySelector('.box')
    window.onload = function() {
      // offsetWidth 会发生重绘
      oBox.style.height = (oBox.offsetWidth / 2) + 'px';
      // 发散思维
    }
    // 移动设备
    window.onresize = function() {
      oBox.style.height = (oBox.offsetWidth / 2) + 'px';
    }
  </script>
</body>
</html>